<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <div th:replace="./widget/base"></div>
</head>
<body>

<div id="jobToolbar">
    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/SYSTEM/API/PAGE_ALL/API_CATEGORY_ADD_DIALOG'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-plus" plain="true"
       onclick="jobAddDialog();">新增</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/SYSTEM/API/PAGE_ALL/API_CATEGORY_EDIT_DIALOG'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-edit" plain="true"
       onclick="jobEditDialog();">编辑</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/QUARTZ/JOB_PAUSE_ALL'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-pause" plain="true"
       onclick="jobPauseAll();">暂停</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/QUARTZ/JOB_PAUSE_ALL2'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-pause" plain="true"
       onclick="jobPauseAll2();">暂停所有</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/QUARTZ/JOB_RESUME_ALL'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-play" plain="true"
       onclick="jobResumeAll();">运行</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/QUARTZ/JOB_RESUME_ALL2'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-play" plain="true"
       onclick="jobResumeAll2();">运行所有</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/QUARTZ/JOB_DELETE_ALL'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-close" plain="true"
       onclick="jobDeleteAll();">删除</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/SYSTEM/API/PAGE_ALL/API_CATEGORY_EDIT_DIALOG'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-edit" plain="true"
       onclick="jobRuntimeLogDialog();">任务运行日志</a>

</div>

<table id="job"></table>

<script type="text/javascript">

    $(function () {

        // 定时任务
        var columns = [
            {field: 'checkbox', checkbox: true},
            {field: 'jobClassName', title: '任务类名'},
            {field: 'jobGroup', title: '任务分组'},
            {field: 'triggerState', title: '任务状态', formatter: function (value, row, index) {
                    if(value === 'ACQUIRED' || value ==='WAITING' || value === 'BLOCKED') {
                        return '运行中';
                    }else {
                    // if(value === 'PAUSED') {
                        return '已暂停';
                    }
                }
            },
            {field: 'cronExpression', title: 'CRON 表达式'},
            {field: 'timeZoneId', title: '时区 ID'},
            {field: 'description', title: '描述'},
        ];

        $('#job').datagrid(expressui.grid.create, {
            title: '定时任务',
            url: Url.Quartz.Api.JOB + '/page_all',
            columns: [columns]
        });
    });


    function jobAddDialog() {

        $('#jobAddDialog').dialog(expressui.dialog.create, {
            title: '新增任务',
            width: 420,
            href: Url.Quartz.Page.JOB + '/add_dialog',
            save: {
                url: Url.Quartz.Api.JOB + '/add_one',
                method: 'post'
            },
            buttons: [{
                text: '保存',
                iconCls: 'fa fa-save',
                handler: expressui.dialog.add,
                reload: [{type: 'datagrid', selector: '#job'}],
                success: '新增成功'
            }, {
                text: '关闭',
                iconCls: 'fa fa-close',
                handler: expressui.dialog.close
            }]
        });
    }

    function jobEditDialog() {
        var checked = $('#job').datagrid(expressui.grid.getCheckedOneOrShowAlert);
        if (checked) {
            $('#jobEditDialog').dialog(expressui.dialog.create, {
                title: '编辑任务',
                grid: {type: 'datagrid', selector: '#job'},
                href: Url.Quartz.Page.JOB + '/edit_dialog',
                get: {url: Url.Quartz.Api.JOB + '/one?jobClassName={jobClassName}&jobGroup={jobGroup}', method: 'get'},
                save: {url: Url.Quartz.Api.JOB + '/save_one', method: 'post'},
                buttons: [{
                    text: '保存',
                    iconCls: 'fa fa-save',
                    handler: expressui.dialog.save,
                    reload: [{type: 'datagrid', selector: '#job'}],
                    success: '保存成功'
                }, {
                    text: '关闭',
                    iconCls: 'fa fa-close',
                    handler: expressui.dialog.close
                }]
            });
        }
    }

    function jobPauseAll() {
        var checked = $('#job').datagrid(expressui.grid.getCheckedLessOneOrShowAlert);
        if(!checked) {
            return;
        }
        var jobClassName = [];
        var jobGroup = [];
        for(var i=0; i<checked.length; i++) {
            jobClassName.push(checked[i].jobClassName);
            jobGroup.push(checked[i].jobGroup);
        }
        $('#job').datagrid(expressui.grid.postCheckedLessOneOrShowAlert, {
            url: Url.Quartz.Api.JOB + '/pause_all',
            method: 'post',
            success: '操作成功',
            data: {'jobClassName[]': jobClassName, 'jobGroup[]': jobGroup},
            reload: [{type: 'datagrid', selector: '#job'}],
        });
    }

    function jobPauseAll2() {
        $('#job').datagrid(expressui.grid.post, {
            url: Url.Quartz.Api.JOB + '/pause_all',
            method: 'post',
            success: '操作成功',
            reload: [{type: 'datagrid', selector: '#job'}],
        });
    }

    function jobResumeAll() {
        var checked = $('#job').datagrid(expressui.grid.getCheckedLessOneOrShowAlert);
        if(!checked) {
            return;
        }
        var jobClassName = [];
        var jobGroup = [];
        for(var i=0; i<checked.length; i++) {
            jobClassName.push(checked[i].jobClassName);
            jobGroup.push(checked[i].jobGroup);
        }

        $('#job').datagrid(expressui.grid.postCheckedLessOneOrShowAlert, {
            url: Url.Quartz.Api.JOB + '/resume_all',
            method: 'post',
            success: '操作成功',
            data: {'jobClassName[]': jobClassName, 'jobGroup[]': jobGroup},
            reload: [{type: 'datagrid', selector: '#job'}],
        });
    }

    function jobResumeAll2() {
        $('#job').datagrid(expressui.grid.post, {
            url: Url.Quartz.Api.JOB + '/resume_all',
            method: 'post',
            success: '操作成功',
            reload: [{type: 'datagrid', selector: '#job'}],
        });
    }

    function jobDeleteAll() {
        var checked = $('#job').datagrid(expressui.grid.getCheckedLessOneOrShowAlert);
        if(!checked) {
            return;
        }

        var jobClassName = [];
        var jobGroup = [];
        for(var i=0; i<checked.length; i++) {
            jobClassName.push(checked[i].jobClassName);
            jobGroup.push(checked[i].jobGroup);
        }

        $('#job').datagrid(expressui.grid.postCheckedLessOneOrShowAlert, {
            url: Url.Quartz.Api.JOB + '/delete_all',
            method: 'post',
            success: '删除成功',
            data: {'jobClassName[]': jobClassName, 'jobGroup[]': jobGroup},
            reload: [{type: 'datagrid', selector: '#job'}],
        });
    }

    function jobRuntimeLogDialog() {
        // var checked = $('#job').treegrid(expressui.grid.getCheckedOneOrShowAlert);
        // if (checked) {
            $('#jobRuntimeLogDialog').dialog(expressui.dialog.create, {
                title: '任务运行日志',
                width:800,
                height:400,
                grid: {type: 'datagrid', selector: '#job'},
                href: Url.Quartz.Page.JOB_RUNTIME_LOG + '/page_all_dialog',
                get: {url: Url.Quartz.Api.JOB_RUNTIME_LOG + '/one?jobClassName={jobClassName}&jobGroup={jobGroup}', method: 'get'},
                save: {url: Url.Quartz.Api.JOB_RUNTIME_LOG + '/save_one', method: 'post'},
                buttons: [{
                    text: '保存',
                    iconCls: 'fa fa-save',
                    handler: expressui.dialog.save,
                    reload: [{type: 'datagrid', selector: '#job'}],
                    success: '保存成功'
                }, {
                    text: '关闭',
                    iconCls: 'fa fa-close',
                    handler: expressui.dialog.close
                }]
            });
        // }
    }

</script>
</body>
</html>